<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/14
 * Time: 14:21
 * 权限管理
 */
namespace app\admin\controller;
use app\models\Bases;
use think\Request;

class Rule extends Base{
    /**
     * @return array|\think\response\View
     * 添加验证规则
     */
    public function ruleAdd(){
        $rule = new Bases('authRule');
        try{
            $list = $rule->selectData('p_id = 0');
            $result['list'] = infinite($list);
            if(Request::instance()->isPost()){
                $post = input('param.');
                $validate = $this->validate($post,'Rule.ruleAdd'); //验证
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                if(!$rule->insertData($post)){
                    throw new \LogicException('操作失败',1010);//插入数据
                }else{
                    $rule->addAdminLog('添加权限管理');
                    return self::formatSuccessResult();
                }

            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('ruleAdd',['result'=>$result]);
    }

    public function ruleList(){
        $ruleModel = new Bases('authRule');
        $result = $ruleModel->selectData();
        $result1['list'] = infinite($result);
        return view('ruleList',['result'=>$result1]);
    }

    /**修改
     * @return array|\think\response\View
     */
    public function ruleSave(){
        $ruleModel = new Bases('authRule');
        try{
            if(Request()->isGet()){
                $get = input('param.');
                $list = $ruleModel->selectData();
                $ruleInfo['list'] = infinite($list);//递归方便重复&nbsp
                $ruleInfo['one'] = $ruleModel->find(['id'=>$get['id']]);
                if(!$ruleInfo){
                    throw new \LogicException('获取数据失败',1011);
                }
            }
            if(Request()->isPost()){
                $post = input('param.');
                $rs = $ruleModel->updateData($post,['id'=>$post['id']]);
                if($rs===false){
                    throw new \LogicException('跟新数据失败',1012);
                }else{
                    $ruleModel->addAdminLog('修改权限管理');
                    return self::formatSuccessResult();
                }

            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('ruleSave',['result'=>$ruleInfo]);
    }

    /**删除
     * @return array
     */
    public function ruleDelete(){
        $ruleModel = new Bases('authRule');
        try{
            $id = input('id');
            $oneInfo = $ruleModel->find(['p_id'=>$id]);
            if(!empty($oneInfo)){
                return self::formatResult(400,'该栏目下有子栏目请先删除子栏目');
            }
            if(!$ruleModel->deleteDaPtaByPrimaryKey($id)){
                throw new \LogicException('删除失败');
            }
            return self::formatSuccessResult();
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }

    }

}